package com.fenghuajueli.libbasecoreui.upload.vivo

import android.app.Application
import android.os.Build
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.fenghuajueli.libbasecoreui.upload.BaseUpload
import com.fenghuajueli.libbasecoreui.upload.Channel
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils
import kotlinx.coroutines.launch

class VivoUploadUtils : BaseUpload() {

    companion object {
        @JvmStatic
        val instance: VivoUploadUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            VivoUploadUtils()
        }
    }

    /**
     * 打开应用时检查
     * 首次安装
     * 次日留存
     * @param lifecycleOwner
     * @param application
     */
    @Deprecated("统一在MyApplication调用", replaceWith = ReplaceWith("openAction(application: Application)"))
    fun openAction(lifecycleOwner: LifecycleOwner, application: Application) {
        lifecycleOwner.action(Channel.VIVO) {
            if (checkIsFirstInstall(application)) {
                MmkvUtils.save(FIRST_LAUNCH, false)
                LogUtils.d("firstInstallAction: 首次安装启动")
                uploadActiveData(application, VivoDataType.ACTIVATION)
            } else if (checkSeconddayRetention()) {
                LogUtils.d("firstInstallAction: 次留")
                uploadActiveData(application, VivoDataType.RETENTION_1)
            }
        }
    }

    fun openAction(application: Application) {
        appScope.launch {
            action(Channel.VIVO) {
                if (checkIsFirstInstall(application)) {
                    MmkvUtils.save(FIRST_LAUNCH, false)
                    LogUtils.d("firstInstallAction: 首次安装启动")
                    uploadActiveData(application, VivoDataType.ACTIVATION)
                } else if (checkSeconddayRetention()) {
                    LogUtils.d("firstInstallAction: 次留")
                    uploadActiveData(application, VivoDataType.RETENTION_1)
                }
            }
        }
    }

    /**
     * 注册
     * 注册完3分钟内算新注册用户
     * @param needCheck 一键登录、微信登录需要传true
     * @param application
     */
    @Deprecated("统一在MyApplication调用", replaceWith = ReplaceWith("registerAction(application: Application)"))
    fun registerAction(lifecycleOwner: LifecycleOwner, application: Application, needCheck: Boolean = false) {
        lifecycleOwner.action(Channel.VIVO) {
            if (needCheck) {
                if (checkIsRegister()) {
                    uploadActiveData(application, VivoDataType.REGISTER)
                }
            } else {
                uploadActiveData(application, VivoDataType.REGISTER)
            }
        }
    }

    fun registerAction(application: Application) {
        appScope.launch {
            action(Channel.VIVO) {
                uploadActiveData(application, VivoDataType.REGISTER)
            }
        }
    }

    /**
     * 付费
     * 7天内付费才上传
     * @param lifecycleOwner
     * @param application
     * @param payAmount
     */
    @Deprecated("统一在MyApplication调用", replaceWith = ReplaceWith("payAction(application: Application, payAmount: Double)"))
    fun payAction(lifecycleOwner: LifecycleOwner, application: Application, payAmount: Double) {
        lifecycleOwner.action(Channel.VIVO) {
            if (checkPayWithinSevendays()) {
                uploadActiveData(application, VivoDataType.PAY, (payAmount * 100).toLong())
            }
        }
    }

    fun payAction(application: Application, payAmount: Double) {
        appScope.launch {
            action(Channel.VIVO) {
                if (checkPayWithinSevendays()) {
                    uploadActiveData(application, VivoDataType.PAY, (payAmount * 100).toLong())
                }
            }
        }
    }

    /**
     * vivo转化数据同步接口
     *
     * @param dataType
     * @param payAmount
     */
    private suspend fun uploadActiveData(application: Application, dataType: VivoDataType, payAmount: Long? = null) {
        application.uploadActiveData(Channel.VIVO) {
            val contentsMap = hashMapOf<String, Any>()
            contentsMap.apply {
                //用户标识类型
                contentsMap["userIdType"] = "OAID"
                //标识的值
                contentsMap["userId"] = oaid ?: ""
                //事件类型
                contentsMap["cvType"] = dataType.name
                //事件发生的系统时间戳
                contentsMap["cvTime"] = System.currentTimeMillis()
                //付费金额
                payAmount?.let { contentsMap["cvParam"] = it }
                //包名
                contentsMap["pkgName"] = application.packageName
                //数据源类型
                contentsMap["srcType"] = "APP"
                //商标
                contentsMap["brand"] = Build.BRAND
                //厂商
                contentsMap["manufacturer"] = Build.MANUFACTURER
                //机型
                contentsMap["model"] = Build.MODEL
            }

            GsonUtils.toJson(contentsMap)
        }
        application.uploadViVoMonitorData(dataType.name) {
            val contentsMap = hashMapOf<String, Any>()
            contentsMap.apply {
                contentsMap["oaid"] = oaid!!
                contentsMap["channel"] = 5
                contentsMap["data_type"] = dataType
                contentsMap["app_pkg"] = application.packageName
                payAmount?.also { contentsMap["pay_amount"] = it }
            }

            GsonUtils.toJson(contentsMap)
        }
    }

}